package com.wei.service;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

/**
 * @program: mybatis_gen
 * @description: 执行策略
 * @author: Mr.chen
 * @create: 2022-04-16 10:44
 **/
public class DoGenerator {
    private static GlobalConfig globalConfig(String projectPath) {
        return new GlobalConfig()
                .setActiveRecord(true)
                .setAuthor("Mr.Chen") //作者
                .setOutputDir(projectPath + "/src/main/java")
//                .setOutputDir("F:\\my_workspace\\mybatis_gen\\src\\main\\java")  //生成路径
                .setFileOverride(true)//是否文件覆盖，如果多次
                .setServiceName("%sService") //设置生成的service接口名首字母是否为I
                .setIdType(IdType.AUTO) //主键策略
                .setBaseResultMap(true)
                .setBaseColumnList(true);
    }

    private static DataSourceConfig dataSourceConfig() {
        return new DataSourceConfig()
                .setDbType(DbType.MYSQL)
                .setDriverName("com.mysql.cj.jdbc.Driver")
                .setUrl("jdbc:mysql://localhost:3306/test")
                .setUsername("root")
                .setPassword("root");
    }

    private static StrategyConfig strategyConfig() {
        return new StrategyConfig()
                .setCapitalMode(true) // 全局大写命名
                .setNaming(NamingStrategy.underline_to_camel) // 数据库表映射到实体的命名策略
                .setInclude("history_visit_log");//生成的表名，多个用逗号隔开
//                .setTablePrefix("tb_"); // 表前缀
    }

    private static PackageConfig packageConfig() {
        return new PackageConfig()
                .setParent("com.wei")//父包名
                .setController("controller")
                .setEntity("entity")
                .setService("service")
                .setServiceImpl("service.impl")
                .setMapper("mapper")
                .setXml("mapper");
    }

    public static void main(String[] args) {
        //5.整合配置
        final String projectPath = System.getProperty("user.dir");
        new AutoGenerator().setGlobalConfig(globalConfig(projectPath))
                .setDataSource(dataSourceConfig())
                .setStrategy(strategyConfig())
                .setTemplateEngine(new FreemarkerTemplateEngine()) //设置模板引擎
                .setPackageInfo(packageConfig())
                .execute();

    }
}
